<?php


namespace app\index\controller;

//use app\validate\User;

use app\facade\User;
use think\Controller;
//use think\Validate;
use think\facade\Validate;
class Demo9 extends Controller
{
    public function test1()
    {
        //要验证的数据
        $data =
            [
                'name' => '张三',
                'email' => 'admin@xiaoniba.com',
                'password' => '123abc',
                'mobile' => '018983638017',
            ];

        $validate = new User();
        if (!$validate->check($data)) {
            return $validate->getError();
        }
        return 'OK';
    }

    public function test2(){

        $data =
            [
                'name' => '张三',
                'email' => 'admin@xiaoniba.com',
                'password' => '123abc',
                'mobile' => '018983638017',
            ];


        if (!User::check($data)) {
            return User::getError();
        }
        return 'OK';
    }

    //调用控制器中的validate方法进行验证:使用用户自定义的验证证器/类
    public function test4(){
        //$this->>validate($data,$validate):返回验证结果
        //要验证的数据
        $data =
            [
                'name' => '张三',
                'email' => 'admin@xiaoniba.com',
                'password' => '123abc',
                'mobile' => '18983638017',
            ];
//        验证规则
        $validate = 'app\validate\User';
        $res = $this->validate($data,$validate);
        if(true !==$res){
            return $res;
        }
        return '验证通过';
    }

    public function test3(){

        $rule =
            [
                'name|姓名' => ['require', 'min' => 2, 'max' => 20],
                'email|邮箱' => ['require', 'email'],
                'password|密码' => ['require', 'min' => 3, 'max' => 12, 'alphaNum'],
                'mobile|手机' => ['require', 'mobile'],

            ];

        $data =
            [
//                'name' => '张三',
                'email' => 'admin@xiaoniba.com',
                'password' => '123abc',
                'mobile' => '18983638017',
            ];

        Validate::rule($rule);
       if(! Validate::check($data)){
            return Validate::getError();
        }
       return 'ok';
    }


}